package com.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.beans.Users;
import com.metier.NombreJourCongesMetier;
import com.metier.UsersMetier;

/**
 * 
 * @author top
 */
public class panelAjouterUtilisateurs extends javax.swing.JPanel {
	private String valeurSelectionner;

	/**
	 * Creates new form panelAjouterUtilisateurs
	 */
	public panelAjouterUtilisateurs() {
		// initComponents();
	}

	public panelAjouterUtilisateurs(JButton boutonAnnuler/*
														 * ,JButton
														 * boutonValider
														 */) {

		initComponents(boutonAnnuler/* ,boutonValider */);
	}

	@SuppressWarnings("unchecked")
	public void initComponents(JButton buttonAnnuler) {

		jLabel1 = new javax.swing.JLabel();
		textNom = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		textPrenom = new javax.swing.JTextField();
		jLabel3 = new javax.swing.JLabel();
		textLogin = new javax.swing.JTextField();
		jLabel4 = new javax.swing.JLabel();
		textPasswords = new javax.swing.JPasswordField();
		jLabel5 = new javax.swing.JLabel();
		textEmail = new javax.swing.JTextField();
		jLabel6 = new javax.swing.JLabel();
		textPhone = new javax.swing.JTextField();
		jLabel7 = new javax.swing.JLabel();
		conboxProfil = new javax.swing.JComboBox();
		jLabel8 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		textAdresse = new javax.swing.JTextArea();
		jLabel9 = new javax.swing.JLabel();
		jLabel10 = new javax.swing.JLabel();
		boutonValider = new javax.swing.JButton();
		boutonAnnuler = new javax.swing.JButton();

		setBackground(new java.awt.Color(128, 130, 133));
		setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1,
				new java.awt.Color(103, 153, 255)));
		setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		setMaximumSize(new java.awt.Dimension(1050, 375));
		setMinimumSize(new java.awt.Dimension(1050, 375));
		setPreferredSize(new java.awt.Dimension(1056, 410));

		jLabel1.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		jLabel1.setText("Nom :");
		jLabel1.setForeground(new java.awt.Color(255, 255, 255));

		textNom.setFont(new java.awt.Font("Verdana", 3, 12)); // NOI18N
		textPrenom.setFont(new java.awt.Font("Verdana", 3, 12)); // NOI18N

		jLabel2.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		jLabel2.setText("Prenom :");
		jLabel2.setForeground(new java.awt.Color(255, 255, 255));

		jLabel3.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		jLabel3.setText("Login :");
		jLabel3.setForeground(new java.awt.Color(255, 255, 255));

		textLogin.setFont(new java.awt.Font("Verdana", 3, 12)); // NOI18N

		jLabel4.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		jLabel4.setText("Passwords :");
		jLabel4.setForeground(new java.awt.Color(255, 255, 255));

		textPasswords.setEditable(false);
		textPasswords.setFont(new java.awt.Font("Verdana", 3, 12)); // NOI18N
		textPasswords.setText("passer");

		jLabel5.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		jLabel5.setText("Email  :");
		jLabel5.setForeground(new java.awt.Color(255, 255, 255));

		textEmail.setFont(new java.awt.Font("Verdana", 3, 12)); // NOI18N

		jLabel6.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		jLabel6.setText(" Phone :");
		jLabel6.setForeground(new java.awt.Color(255, 255, 255));

		textPhone.setFont(new java.awt.Font("Verdana", 3, 12)); // NOI18N

		textPhone.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyTyped(java.awt.event.KeyEvent e) {

				if ((e.getKeyChar() > '9') || (e.getKeyChar() < '0'))
					e.setKeyChar('\0');
			}

		});
		textNom.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

			}
		});
		jLabel7.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		jLabel7.setText(" Profil :");
		jLabel7.setForeground(new java.awt.Color(255, 255, 255));

		// conboxProfil.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
		conboxProfil.setPreferredSize(new Dimension(100, 20));
		conboxProfil.addItem("Profil");
		conboxProfil.addItem("Administrateur");
		conboxProfil.addItem("Agent Commercial");
		conboxProfil.addItem("Comptable");
		conboxProfil.addItem("Consultant");

		// conboxProfil.setMaximumSize(new java.awt.Dimension(173, 24));
		conboxProfil.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {

				Object source = conboxProfil.getSelectedItem();
				valeurSelectionner = (String) source;

			}
		});

		jLabel8.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		jLabel8.setText("Adresse :");
		jLabel8.setForeground(new java.awt.Color(255, 255, 255));

		textAdresse.setColumns(20);
		textAdresse.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		textAdresse.setRows(3);
		jScrollPane1.setViewportView(textAdresse);

		jLabel9.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/profil.gif")); // NOI18N

		jLabel10.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		jLabel10.setText("Photo de Profil");
		jLabel10.setForeground(new java.awt.Color(255, 255, 255));

		boutonValider.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		boutonValider.setText("VALIDER");
		boutonValider.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				PanelConsultant pan = new PanelConsultant();
				boolean areEmpty;
				int indexe = -1;
				areEmpty = false;
				if (conboxProfil.getSelectedItem().equals("Profil")) {
					indexe = 0;

				} else if (conboxProfil.getSelectedItem().equals(
						"Administrateur")) {
					indexe = 1;

				} else if (conboxProfil.getSelectedItem().equals(
						"Agent Commercial")) {
					indexe = 2;
				} else if (conboxProfil.getSelectedItem().equals("Comptable")) {
					indexe = 3;
				} else if (conboxProfil.getSelectedItem().equals("Consultant")) {
					indexe = 4;
				} else {
					indexe = -1;
				}
				areEmpty = (!textPrenom.getText().isEmpty()
						&& !textNom.getText().isEmpty()
						&& !textLogin.getText().isEmpty()
						&& !textPasswords.getText().isEmpty()
						&& !textAdresse.getText().isEmpty()
						&& !textPhone.getText().isEmpty() && !(indexe == -1)
						&& !textEmail.getText().isEmpty() && !(indexe == 0));
				if (!areEmpty) {

					JOptionPane.showMessageDialog(boutonValider,
							"veuillez remplir tous les champs");
				} else {

					UsersMetier userMetier = new UsersMetier();
					userMetier.addUsersMetier(textNom.getText(),
							textPrenom.getText(), textLogin.getText(),
							textEmail.getText(), textPasswords.getText(),
							textPhone.getText(), valeurSelectionner,
							textAdresse.getText());
					pan.getPanelbas().removeAll();
					tableListUsers();
					pan.getPanelbas().add(new JScrollPane(table),
							BorderLayout.CENTER);
					validate();

					textPhone.setText("");
					textAdresse.setText("");
					textEmail.setText("");
					textNom.setText("");
					textLogin.setText("");
					textPrenom.setText("");
					textPhone.setText("");
					JOptionPane.showMessageDialog(null,
							"L'utilisateur a été bien enregistré ");
					NombreJourCongesMetier nbrJrMetier = new NombreJourCongesMetier();
					nbrJrMetier.addNbrJrCng(24);

				}

			}
			// }
		});

		boutonAnnuler.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		boutonAnnuler.setText("ANNULER");
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(68, 68, 68)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING,
												false)
												.addComponent(
														jLabel5,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jLabel4,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jLabel3,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jLabel2,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jLabel1,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addGap(27, 27, 27)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		boutonValider)
																.addGap(26, 26,
																		26)
																.addComponent(
																		buttonAnnuler)
																.addContainerGap(
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		Short.MAX_VALUE))
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						textLogin,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						190,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addComponent(
																														textPrenom,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														190,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addGroup(
																														layout.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING,
																																false)
																																.addComponent(
																																		textEmail,
																																		javax.swing.GroupLayout.Alignment.TRAILING)
																																.addComponent(
																																		textNom)
																																.addComponent(
																																		textPasswords,
																																		javax.swing.GroupLayout.DEFAULT_SIZE,
																																		190,
																																		Short.MAX_VALUE)))
																								.addGap(72,
																										72,
																										72)
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING,
																												false)
																												.addComponent(
																														jLabel6,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														Short.MAX_VALUE)
																												.addComponent(
																														jLabel8,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														81,
																														Short.MAX_VALUE)
																												.addComponent(
																														jLabel7,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														Short.MAX_VALUE))))
																.addGap(18, 18,
																		18)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																				.addComponent(
																						jScrollPane1,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						189,
																						Short.MAX_VALUE)
																				.addComponent(
																						textPhone)
																				.addComponent(
																						conboxProfil,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						127,
																						javax.swing.GroupLayout.PREFERRED_SIZE))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																		89,
																		Short.MAX_VALUE)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						jLabel9)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGap(10,
																										10,
																										10)
																								.addComponent(
																										jLabel10,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										143,
																										javax.swing.GroupLayout.PREFERRED_SIZE)))
																.addGap(65, 65,
																		65)))));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(68, 68,
																		68)
																.addComponent(
																		jLabel9,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		177,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																.addComponent(
																		jLabel10))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(65, 65,
																		65)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.BASELINE)
																				.addComponent(
																						jLabel1,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						30,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						textNom,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						30,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						jLabel6,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						30,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						textPhone,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						25,
																						javax.swing.GroupLayout.PREFERRED_SIZE))
																.addGap(6, 6, 6)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.BASELINE)
																												.addComponent(
																														textPrenom,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														30,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														jLabel2,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														30,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														jLabel8,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														24,
																														javax.swing.GroupLayout.PREFERRED_SIZE))
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addComponent(
																														textLogin,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														30,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														jLabel3,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														30,
																														javax.swing.GroupLayout.PREFERRED_SIZE)))
																				.addComponent(
																						jScrollPane1,
																						javax.swing.GroupLayout.Alignment.TRAILING,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						73,
																						javax.swing.GroupLayout.PREFERRED_SIZE))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						textPasswords,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						30,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						jLabel4,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						30,
																						javax.swing.GroupLayout.PREFERRED_SIZE))
																.addGap(18, 18,
																		18)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.BASELINE)
																				.addComponent(
																						jLabel5,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						33,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						textEmail,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						33,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						jLabel7,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						30,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						conboxProfil,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						33,
																						javax.swing.GroupLayout.PREFERRED_SIZE))))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										57, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(boutonValider)
												.addComponent(buttonAnnuler))
								.addGap(50, 50, 50)));
	}// </editor-fold>//GEN-END:initComponents

	public JPanel getFrame() {
		return this;
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton boutonValider;
	private javax.swing.JComboBox conboxProfil;
	private javax.swing.JButton boutonAnnuler;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel10;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextArea textAdresse;
	private javax.swing.JTextField textEmail;
	private javax.swing.JTextField textLogin;
	private javax.swing.JTextField textNom;
	private javax.swing.JPasswordField textPasswords;
	private javax.swing.JTextField textPhone;
	private javax.swing.JTextField textPrenom;
	private DefaultTableModel dtm;
	private JTable table;

	public String getValeurSelectionner() {
		return valeurSelectionner;
	}

	public void setValeurSelectionner(String valeurSelectionner) {
		this.valeurSelectionner = valeurSelectionner;
	}

	public javax.swing.JComboBox getConboxProfil() {
		return conboxProfil;
	}

	public void setConboxProfil(javax.swing.JComboBox conboxProfil) {
		this.conboxProfil = conboxProfil;
	}

	public javax.swing.JScrollPane getjScrollPane1() {
		return jScrollPane1;
	}

	public void setjScrollPane1(javax.swing.JScrollPane jScrollPane1) {
		this.jScrollPane1 = jScrollPane1;
	}

	public javax.swing.JTextArea getTextAdresse() {
		return textAdresse;
	}

	public void setTextAdresse(javax.swing.JTextArea textAdresse) {
		this.textAdresse = textAdresse;
	}

	public javax.swing.JTextField getTextEmail() {
		return textEmail;
	}

	// by weuz
	// méthode qui permet de charger les users dans un JTABLE

	public void tableListUsers() {

		String[] entetes = { "NOM", "Prenom", "Login", "E-Mail", "Telephone",
				"Profil", "Adresse" };
		UsersMetier usersMetier = new UsersMetier();
		List<Users> listUsers = usersMetier.chargerAllUsers();
		int nombreLigne = listUsers.size();
		System.out.println("taille de la liste" + listUsers.size());
		Object data[][] = new Object[nombreLigne][7];

		// On parcoure la liste pour recuperer les users et remplir le tableau
		// d'objet
		for (int i = 0; i < nombreLigne; i++) {
			Users users = new Users();
			users = listUsers.get(i);

			data[i][0] = users.getUsrNom();
			data[i][1] = users.getUsrPrenom();

			data[i][2] = users.getUsrLogin();
			data[i][3] = users.getUsrEmail();

			data[i][4] = users.getUsrPhone();
			data[i][5] = users.getUsrProfil();
			data[i][6] = users.getUsrAdresse();

		}

		dtm = new DefaultTableModel(data, entetes);

		table = new JTable(dtm);

	}

	public void setTextEmail(javax.swing.JTextField textEmail) {
		this.textEmail = textEmail;
	}

	public javax.swing.JTextField getTextLogin() {
		return textLogin;
	}

	public void setTextLogin(javax.swing.JTextField textLogin) {
		this.textLogin = textLogin;
	}

	public javax.swing.JTextField getTextNom() {
		return textNom;
	}

	public void setTextNom(javax.swing.JTextField textNom) {
		this.textNom = textNom;
	}

	public javax.swing.JPasswordField getTextPasswords() {
		return textPasswords;
	}

	public void setTextPasswords(javax.swing.JPasswordField textPasswords) {
		this.textPasswords = textPasswords;
	}

	public javax.swing.JTextField getTextPhone() {
		return textPhone;
	}

	public void setTextPhone(javax.swing.JTextField textPhone) {
		this.textPhone = textPhone;
	}

	public javax.swing.JTextField getTextPrenom() {
		return textPrenom;
	}

	public void setTextPrenom(javax.swing.JTextField textPrenom) {
		this.textPrenom = textPrenom;
	}

	// End of variables declaration//GEN-END:variables
}
